<?php

namespace app\admin\validate;

use app\common\lib\Captcha;
use think\Validate;

class User extends Validate
{
    protected $rule = [
        'username' => 'require',
        'password' => 'require',
        'code' => 'require|checkCaptcha',
        'status'=>'require|checkStatus',
        'roles'=>'require',
        'real_name'=>'require',
        'id'=>'require'
    ];

    protected $scene=[
        'login'=>['username','password','code'],
        'create'=>['status','roles','password','real_name','username'],
        'update'=>['id','status','roles','real_name','username'],
        'pwd'=>['id','password']
    ];

    protected $message = [
        'username' => '用户名不能为空',
        'password' => '密码不能为空',
        'code' => '验证码不能为空',
        'status' => '状态不能为空',
        'roles' => '角色不能为空',
        'real_name' => '名称不能为空',
    ];

    protected function checkCaptcha($value, $rule, $data = [])
    {
        if (!app()->make(Captcha::class)->check($value,$data['key'])) {
            return "您输入的验证码不正确！";
        }
        return true;
    }
    protected function checkStatus($value){
        if(!in_array($value,[0,1])){
            return '状态错误';
        }
        return true;
    }
}
